# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
from datetime import datetime

import openpyxl
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from twisted.enterprise import adbapi

class XingzhengquhuaPipeline:
    def process_item(self, item, spider):
        if(spider.name != 'xzqh'):
            return item
        print(item)


class XingzhenxukegonggaoPipeline:
    def __init__(self):
        super().__init__()
        self.wb = openpyxl.Workbook()
        self.ws = self.wb.active
        self.ws.append(['社会组织名称', '统一社会信用代码', '许可名称', '许可事项(前)', '许可事项(后)','法定代表人', '批准或核准时间'])

    def process_item(self, item, spider):
        if(spider.name != 'xzxkgg'):
            return item
        self.ws.append([item['orgName'], item['unifiedCode'], item['nodeName'], item['modifyBefore'], item['modifyAfter'], item['legalName'], item['approveTime']])


    def close_spider(self, spider):
        if (spider.name != 'xzxkgg'):
            return
        current_date = datetime.now().strftime('%Y-%m-%d')
        self.wb.save(filename=f'行政许可公告_{current_date}.xlsx')
        self.wb.close()

class XingzhenchufagonggaoPipeline:
    def __init__(self):
        super().__init__()
        self.wb = openpyxl.Workbook()
        self.ws = self.wb.active
        self.ws.append(['社会组织名称', '统一社会信用代码', '行政处罚决定书文号', '处罚结果', '处罚决定日期', '处罚机关', '处罚依据', '处罚事由'])

    def process_item(self, item, spider):
        if(spider.name != 'xzcfgg'):
            return item
        self.ws.append([item['orgName'], item['usci'], item['caseNo'], item['punishName'], item['punishTime'],
                        item['punishOrgan'], item['punishBasis'], item['punishReason']])
        print(item)

    def close_spider(self, spider):
        if (spider.name != 'xzcfgg'):
            return
        current_date = datetime.now().strftime('%Y-%m-%d')
        self.wb.save(filename=f'行政处罚公告_{current_date}.xlsx')
        self.wb.close()
